package com.pd.kit;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by dong peng on 2018/4/30.
 */
public class JedisPoolUtil {
    private static JedisPool jedisPool = null;

    public static Jedis getInstance() {
        if(jedisPool == null) {
            synchronized (JedisPoolUtil.class) {
                if(jedisPool == null) {
                    new JedisPoolUtil();
                }
            }
        }

        return jedisPool.getResource();
    }


    private JedisPoolUtil() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(200);
        jedisPoolConfig.setMaxIdle(50);
        jedisPoolConfig.setMinIdle(8);//设置最小空闲数
        jedisPoolConfig.setMaxWaitMillis(10000);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPoolConfig.setTestOnReturn(true);
        //Idle时进行连接扫描
        jedisPoolConfig.setTestWhileIdle(true);
        //表示idle object evitor两次扫描之间要sleep的毫秒数
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
        //表示idle object evitor每次扫描的最多的对象数
        jedisPoolConfig.setNumTestsPerEvictionRun(10);
        //表示一个对象至少停留在idle状态的最短时间，然后才能被idle object evitor扫描并驱逐；这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
        jedisPoolConfig.setMinEvictableIdleTimeMillis(60000);
        String host = "127.0.0.1";
        String password = null;
        Integer port = 6379;
        Integer timeout = 0;

        jedisPool = new redis.clients.jedis.JedisPool(jedisPoolConfig, host, port, timeout, password, 0);
    }

}
